package Noyau;

import java.awt.geom.Point2D;

public class Toboggan extends CheminBagage
{
	@Override
    public boolean PeutAjouterBagage(Bagage newBagage)
    {
    	// We want to make sure that we have enough room left to fit the new bagage.
		// We retrieve the last bagage and compute it's position.		
		return true;
    }
	
	@Override	
	public boolean AjouterBagage(Bagage newBagage) {
		if (!this.PeutAjouterBagage(newBagage))
		{
			return false;
		}
		
		// Otherwise, we add the bagage to the list.
		newBagage.setPosition(0);
		this.CreerBagage(newBagage);
		
		return true;
	}

	@Override
	public void TickHorloge() {	
		// We retrieve our size.
		double size = this.getSize();
		
		// For each bagage (sorted by highest position, FIFO), we try to
		// make them move, if it's possible.		
		int index = 0;
		while (index < this.bagages.size())
		{
			// We retrieve the bagage at Index index.
			Bagage bagage = this.bagages.get(index);
			
			// We compute the new position of this bagage.
			double position = bagage.getPosition();
			position += 0.06 / size;			
			
			if (position > 1)
			{				
				this.bagages.remove(bagage);
			}
			else
			{				
				// We update the position of the bagage.				
				bagage.setPosition(position);
				bagage.setOpacity(1 - position);
				
				index++;	
			}
		}

    	this.setChanged();
    	this.notifyObservers();
	}
	
	@Override
	public Point2D getCenter()
	{
		double dist = -this.getVisualSize() / 2;
		double rotX = (Math.cos(this.getAngle()) * dist) + this.getOrigine().getX();
		double rotY = (Math.sin(this.getAngle()) * dist) + this.getOrigine().getY();
		
		return new Point2D.Double(rotX, rotY);
	}
	
	@Override
	public double getAngle()
	{
		if (this.entree == null)
		{
			return 0;
		}
		
		// Custom hack when the entree of the Toboggan is a lien (to have the right orientation).
		double baseAngle = super.getAngle();
		
		if (this.entree instanceof CheminLien)
		{
			baseAngle += (Math.PI / 2);
		}
		
		return baseAngle;
	}
	
	@Override
	public double getSize()
	{
		return 5;
	}
	
	@Override
	public double getVisualSize()
	{
		return 5;
	}
	
	@Override
	public int getZBuffer()
	{
		return 0;
	}
}
